package com.hzf.booklibrary.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnErrorListener;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.hzf.booklibrary.R;
import com.hzf.booklibrary.base.BaseActivity;
import com.hzf.booklibrary.component.AppComponent;
import com.hzf.booklibrary.utils.LogUtils;
import com.hzf.booklibrary.utils.ToastUtils;
import com.hzf.booklibrary.view.PdfViewerGroup;

import java.io.File;
import java.lang.reflect.Field;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/10.
 */

public class PDFReadActivity extends BaseActivity implements OnLoadCompleteListener, OnErrorListener, OnPageChangeListener, PdfViewerGroup.OnReadPDFSateChangeListener {
    public static final String PDF_READ="pdfRead";
    public static void start(File file, Context context){
        Intent intent = new Intent(context,PDFReadActivity.class);
        intent.putExtra(PDF_READ,file);
        context.startActivity(intent);
    }
    public static void start(Context context,File file,String bookName) {
        Intent intent = new Intent(context, PDFReadActivity.class);
        intent.putExtra(PDF_READ,file);
        intent.putExtra("bookName",bookName);
        context.startActivity(intent);
    }
    @Bind(R.id.pdfView)
    PDFView pdfView;
    @Bind(R.id.pdf_group)
    PdfViewerGroup pdfViewerGroup;
    @Bind(R.id.seekBarProgress)
    SeekBar seekBarProgress;
    @Bind(R.id.reBookReadTop)
    RelativeLayout reBookReadTop;
    @Bind(R.id.tvBookReadTocTitle)
    TextView tvBookReadTocTitle;
    @Bind(R.id.reBookReadBottom)
    RelativeLayout reBookReadBottom;
    File localFile;
    @Override
    public int getLayoutId() {
        return R.layout.activity_pdf_read;

    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    public void initTitleBar() {
        hideActionBar();
        tvBookReadTocTitle.setText(getIntent().getStringExtra("bookName"));
    }

    @Override
    public void initDatas() {
        localFile = (File) getIntent().getSerializableExtra(PDF_READ);
    }

    @Override
    public void configViews() {
        pdfViewerGroup.setOnReadPDFSateChangeListener(new PdfViewerGroup.OnReadPDFSateChangeListener() {
            @Override
            public void onCenterClick() {
                ToastUtils.showSingleToast("点击了中间位置");
            }
        });
        if(localFile!=null){
            pdfView.fromFile(localFile)
                    .defaultPage(0)
                    .enableDoubletap(false)
                    .enableAnnotationRendering(true)
                    .swipeHorizontal(true)
                    .scrollHandle(new DefaultScrollHandle(this))
                    .spacing(10)
                    .onLoad(this)
                    .onError(this)
                    .onPageChange(this)
                    .load();
        }
        pdfViewerGroup.setOnReadPDFSateChangeListener(this);
    }

    @Override
    public void loadComplete(int nbPages) {
        int pageCount = pdfView.getPageCount();
        LogUtils.e("pageCount:"+pageCount);
        seekBarProgress.setMax(pageCount-1);
        seekBarProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                pdfView.jumpTo(progress);
            }
        });
    }

    @Override
    public void onError(Throwable t) {
        ToastUtils.showSingleLongToast("加载失败，请清除缓存后重试！");
        finish();
    }

    @Override
    public void onPageChanged(int page, int pageCount) {

    }

    @Override
    public void onCenterClick() {
        if(isVisible(reBookReadTop)){
            hideActionBar();
        }else {
            showActionBar();
        }
    }
    @OnClick(R.id.ibBackTop)
    public void onClickBackTop(){
        finish();
    }
    @OnClick(R.id.ibBackBottom)
    public void onClickBackBottom(){
        finish();
    }
    private void showActionBar(){
        seekBarProgress.setProgress(pdfView.getCurrentPage());
        visible(reBookReadBottom,reBookReadTop);
    }
    private void hideActionBar(){
        gone(reBookReadBottom,reBookReadTop);
    }
}
